package com.llly.controller;

import com.github.pagehelper.Page;
import com.llly.client.NewsFeignClient;
import com.llly.pojo.Easybuy_news;
import com.llly.pojo.Easybuy_user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
public class NewsController {


    @Autowired
    private NewsFeignClient newsFeignClient;

    //后台新闻管理页面.
    @RequestMapping("/newsShow")
    public ModelAndView newsShow(ModelAndView mv, HttpSession session,Integer pageIndex){
        if (pageIndex==null){
            pageIndex=1;
        }
        Easybuy_user user = (Easybuy_user)session.getAttribute("user");
        Map<String, Object> stringObjectMap = newsFeignClient.ShowNews(pageIndex);
        List<Easybuy_news> news = (List<Easybuy_news>) stringObjectMap.get("pg");
        mv.addObject("news",news);
        mv.addObject("Total",stringObjectMap.get("Total"));
        mv.addObject("PageNum",stringObjectMap.get("PageNum"));
        mv.addObject("Pages",stringObjectMap.get("Pages"));
        Integer pages = (Integer) stringObjectMap.get("Pages");
        List pag=new ArrayList();
        for (int i=1; i<=pages; i++){
            pag.add(i);
        }
        mv.addObject("listPage",pag);
        if ((user.getType()).equals(1)){
            mv.setViewName("Manager/news");
        }else {
            mv.setViewName("UserNews");
        }
        return mv;
    }


    //进入详情页面
    @RequestMapping("/newsView")
    public ModelAndView newsView(ModelAndView mv, HttpSession session,Integer id){
        System.out.println("8081******controller******详情方法");
        Easybuy_user user = (Easybuy_user)session.getAttribute("user");
        Easybuy_news newsView = newsFeignClient.newsView(id);
        mv.addObject("views",newsView);
        System.out.println("user++++++类型"+user.getType());
        mv.setViewName("Manager/newsview");
        return mv;
    }

    //进入新闻添加跳转页面
    @RequestMapping("/addnews")
    public ModelAndView addnews(ModelAndView mv){
        System.out.println("****进入8081新闻添加页面******");
        mv.setViewName("Manager/news-add");

        return mv;
    }
    //进入新闻添加页面
    @RequestMapping("/addnews2")
    public ModelAndView addnews2(ModelAndView mv,Easybuy_news en){
        System.out.println("55555555555"+en);
        int addnews = newsFeignClient.addnews(en);
        System.out.println("55555555555"+addnews);
        if (addnews>0){
            mv.setViewName("forward:/newsShow");
        }else {
        mv.setViewName("Manager/news-add");
    }
        return mv;
    }

    //删除
    @RequestMapping("/delnews")
    public ModelAndView delnews(Integer id,ModelAndView mv){
        System.out.println("****进入8081新闻删除页面******");
        int delnews = newsFeignClient.delnews(id);
        System.out.println("*****获取到删除用户的id是:"+delnews);
        if (delnews>0){
            mv.addObject("messa",1);
            mv.setViewName("forward:/newsShow");
        }else {
            mv.addObject("messa",0);
            mv.setViewName("Manager/news");
        }
        return mv;
    }

    //管理员页面用id进行查询回显
    @RequestMapping("/findById")
    public ModelAndView findById(Integer id,ModelAndView mv){
        Easybuy_news byId = newsFeignClient.findById(id);
        mv.addObject("byId",byId);
        mv.setViewName("Manager/news-modify");
        return mv;
    }

    //修改
    @RequestMapping("/updatenews")
    public ModelAndView updatenews( Easybuy_news en,ModelAndView mv){
        System.out.println("****进入8081新闻修改页面******");
        int updatenews = newsFeignClient.updatenews(en);
        if (updatenews>0){
            mv.setViewName("forward:/newsShow");
        }else {
            mv.setViewName("Manager/news-modify");
        }
        return mv;
    }
}
